package com.example.mybatisplusdemo.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mybatisplusdemo.Methods.methods;
import com.example.mybatisplusdemo.Result.Result;
import com.example.mybatisplusdemo.Service.AdminService;
import com.example.mybatisplusdemo.Service.BusinessService;
import com.example.mybatisplusdemo.Service.UserService;
import com.example.mybatisplusdemo.pojo.Admin;
import com.example.mybatisplusdemo.pojo.Business;
import com.example.mybatisplusdemo.pojo.User;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;


@RestController
@RequestMapping("/api")
@CrossOrigin
@Validated
public class LoginController {
    @Resource
    AdminService adminService;
    @Resource
    BusinessService businessService;
    @Resource
    UserService userService;

    /**
     * 管理员登录方法
     * @param loginname
     * @param pwd
     * @return
     */
    @PostMapping("/adminlogin")
    public Result adminlogin(@Valid @NotBlank(message = "用户名不能为空") @RequestParam("loginname") String loginname,
                             @Valid @NotBlank(message = "密码不能为空")  @RequestParam("pwd") String pwd){
        pwd = new methods().md5(pwd);
        int count = adminService.count(new QueryWrapper<Admin>().eq("loginname",loginname).eq("pwd",pwd));
        if (count !=0){
            Admin admin = adminService.getOne(new QueryWrapper<Admin>().eq("loginname",loginname).eq("pwd",pwd));
            return new Result(200,"登录成功",admin);
        }else{
            return new Result(400,"登录失败");
        }
    }

    /**
     * 商家登录方法
     * @param loginname
     * @param pwd
     * @return
     */
    @PostMapping("/businesslogin")
    public Result businesslogin(@Valid @NotBlank(message = "用户名不能为空") @RequestParam("loginname") String loginname,
                                @Valid @NotBlank(message = "密码不能为空") @RequestParam("pwd") String pwd){
        pwd = new methods().md5(pwd);
        int count = businessService.count(new QueryWrapper<Business>().eq("loginname",loginname).eq("pwd",pwd));
        if (count !=0){
            Business business = businessService.getOne(new QueryWrapper<Business>().eq("loginname",loginname).eq("pwd",pwd));
            return new Result(200,"登录成功",business);
        }else{
            return new Result(400,"登录失败");
        }
    }

    /**
     * 前台用户登录
     * @param loginname
     * @param pwd
     * @return
     */
    @PostMapping("/userlogin")
    public Result userlogin(@Valid @NotBlank(message = "用户名不能为空") @RequestParam("loginname") String loginname,
                            @Valid @NotBlank(message = "密码不能为空") @RequestParam("pwd") String pwd){
        pwd = new methods().md5(pwd);
        int count = userService.count(new QueryWrapper<User>().eq("loginname",loginname).eq("pwd",pwd));
        if (count !=0){
            User user = userService.getOne(new QueryWrapper<User>().eq("loginname",loginname).eq("pwd",pwd));
            return new Result(200,"登录成功",user);
        }else{
            return new Result(400,"登录失败");
        }
    }
}
